﻿using MKAdminCore.DTO.Web.Common;
using MKAdminCore.IService.Web.TableData;
using MKAdminCore.ToolKit;
using MKAdminCore.ToolKit.StringHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using MKAdminCore.DTO.Web.TableData.BasicTable;
using MKAdminCore.Service.Web.Common;

namespace MKAdminCore.ServiceImpl.Web.TableData
{
    public class BasicTableService : IBasicTableService
    {
        /// <summary>
        /// 获取员工信息列表
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public PagingList<GetBasicTableListModel> List(GetBasicTableListParameter parameter)
        {
            var list = new PagingList<GetBasicTableListModel>();

            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendLine(@"SELECT EmployeeId, EmployeeName,EmployeeSex,EmployeeAge
                                        ,EmployeeHeight,ContactPhone,Education
                                        ,EntryTime,CreateTime 
                                        FROM DBO.EmployeeInfo eli With(Nolock) 
                                Where Status In (0,1)");

                var param = new DynamicParameters();
                if (!string.IsNullOrEmpty(parameter.name))
                {
                    sql.AppendLine("And eli.EmployeeName Like '%'+@EmployeeName+'%'");
                    param.Add("@EmployeeName", parameter.name);
                }

                string sortingSql = "Order By EmployeeId Desc";
                if (!string.IsNullOrEmpty(parameter.sortName) && !string.IsNullOrEmpty(parameter.sortOrder))
                {
                    sortingSql = $"Order By {parameter.sortName} {parameter.sortOrder}";
                }

                list = conn.QueryGetList<GetBasicTableListModel>(sql.ToString(), sortingSql, parameter.pageIndex, parameter.pageSize, param);

                return list;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public async Task<HttpResponseResult> Add(AddBasicTableParam parameter, UserInfo userInfo)
        {
            var result = new HttpResponseResult() { Msg = "新增成功" };

            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendLine(@"INSERT INTO [dbo].[EmployeeInfo]
                                       ([EmployeeName],[EmployeeSex],[EmployeeAge],[EmployeeHeight]
                                         ,[EmployeeWeight],[PayMoney],[HeadFileName],[ContactPhone]
                                         ,[Education],[AddressDetail],[Hobby],[Motto]
                                         ,CreatorId,[StatusCode],[Status],[EntryTime]
                                         ,[CreateTime],[UpdateTime],[Remark])
                                 VALUES
                                       (@EmployeeName,@EmployeeSex,@EmployeeAge,@EmployeeHeight
                                       ,55,0,'',@ContactPhone
                                       ,@Education,'','',''
                                       ,@CreatorId,0,0,getdate()
                                       ,getdate(),getdate(),'')");

                var param = new DynamicParameters();
                param.Add("@EmployeeName", parameter.employeeName);
                param.Add("@EmployeeSex", parameter.employeeSex);
                param.Add("@EmployeeAge", parameter.employeeAge);
                param.Add("@EmployeeHeight", parameter.employeeHeight);
                param.Add("@ContactPhone", parameter.employeePhone);
                param.Add("@Education", parameter.employeeEducation);
                param.Add("@CreatorId", userInfo.OperatorId);

                int _r = await conn.ExecuteAsync(sql.ToString(), param);
                if (_r <= 0)
                {
                    result.Code = (int)HttpResponseCode.OperateFail;
                    result.Msg = "新增失败";
                }
                return result;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public async Task<HttpResponseResult> Edit(EditBasicTableParam parameter)
        {
            var result = new HttpResponseResult() { Msg = "修改成功" };

            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendLine(@"Update [dbo].[EmployeeInfo]
                                 Set [EmployeeName] = @EmployeeName
                                     ,[EmployeeSex] = @EmployeeSex
                                     ,[EmployeeAge] = @EmployeeAge
                                     ,[EmployeeHeight] = @EmployeeHeight
                               ,[ContactPhone] = @ContactPhone
                                     ,[Education] = @Education
                               ,[UpdateTime] = GetDate()
                                 Where EmployeeId = @EmployeeId");

                var param = new DynamicParameters();
                param.Add("@EmployeeId", parameter.employeeId);
                param.Add("@EmployeeName", parameter.employeeName);
                param.Add("@EmployeeSex", parameter.employeeSex);
                param.Add("@EmployeeAge", parameter.employeeAge);
                param.Add("@EmployeeHeight", parameter.employeeHeight);
                param.Add("@ContactPhone", parameter.employeePhone);
                param.Add("@Education", parameter.employeeEducation);

                int _r = await conn.ExecuteAsync(sql.ToString(), param);
                if (_r <= 0)
                {
                    result.Code = (int)HttpResponseCode.OperateFail;
                    result.Msg = "修改失败";
                }
                return result;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public async Task<HttpResponseResult> Del(DelBasicTableParam parameter)
        {
            var result = new HttpResponseResult() { Msg = "删除成功" };

            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendLine($@"Update [dbo].[EmployeeInfo]
                                  Set Status = 2 , UpdateTime = GetDate()
                                  Where EmployeeId = {parameter.employeeId}");


                int _r = await conn.ExecuteAsync(sql.ToString());
                if (_r <= 0)
                {
                    result.Code = (int)HttpResponseCode.OperateFail;
                    result.Msg = "删除失败";
                }
                return result;
            }
        }
    }
}
